package com.eduhzy.ttw.clazz.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.mvp.model.entity.ClazzReviewIcon;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.decoration.GridSpacingItemDecoration;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-5 13:25:25
 */
@Route(path = RouterHub.CLAZZ_CHOOSEICONACTIVITY)
public class ChooseIconActivity extends BaseActivity implements IView {

    @BindView(R2.id.rv_list)
    RecyclerView mRvList;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.clazz_activity_choose_icon;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        ArrayList<ClazzReviewIcon> lists = getIntent().getParcelableArrayListExtra(Constants.PARCELABLE_LIST);
        if (ObjectUtils.isEmpty(lists)) {
            finish();
        }
        mRvList.setLayoutManager(new GridLayoutManager(this, 4));
        mRvList.addItemDecoration(new GridSpacingItemDecoration(4, RxUtil.getAutoWidth(this, 20), true));
        BaseQuickAdapter<ClazzReviewIcon, AutoBaseViewHolder> adapter = new BaseQuickAdapter<ClazzReviewIcon, AutoBaseViewHolder>(R.layout.clazz_review_item_score, lists) {
            @Override
            protected void convert(AutoBaseViewHolder helper, ClazzReviewIcon item) {
                helper.setGone(R.id.btn_del, false);
                helper.setGone(R.id.tv_name, false);
                Glide.with(mContext)
                        .load(item.getUrl(mContext))
                        .into((ImageView) helper.getView(R.id.iv_icon));
            }
        };
        mRvList.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            Message msg = Message.obtain();
            msg.obj = adapter.getData().get(position);
            msg.what = EventBusTags.CHOOSE_REVIEW_ICON;
            EventBus.getDefault().post(msg, RouterHub.CLAZZ_ADDREVIEWACTIVITY);
            finish();
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    public Activity getActivity() {
        return this;
    }

}
